package com.bayss.core.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 短信发送工具类
 */
public class SendSmsUtil {
    //常量参数最好配置到配置文件中
    private static String accessKeyId = "LTAI4FjvBA5HR1Yk4xf6AERV";
    private static String accessSecret = "oKL3jHhk4pX4nL8rZrvHuNPSkyhC0C";
    private static String signName = "dokopay2020";//签名管理里的签名名称

    private static boolean send(String PhoneNumbers,String templateCode,String templateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean sendVailCode(String PhoneNumbers, String code) {
        return SendSmsUtil.send(PhoneNumbers,"SMS_184211112", "{\"code\":\""+code+"\"}");
    }

    public static void main(String[] args) {
        String PhoneNumbers = "18510649276";//多个用逗号分割13776670000,13776670000
        String templateCode = "SMS_184211112";//短信模板code
        String templateParam = "{\"code\":\"2155\"}";//"{\"name\":\"明城墙折扣票\",\"code\":\"2155\",\"time\":\"2019-09-10\"}";//短信模板内参数
        boolean sendStatus = SendSmsUtil.send(PhoneNumbers,templateCode,templateParam);
        if(sendStatus){
            System.out.println("短信发送成功");
        }
    }


}
